package study.lanqiao.com;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;

import java.util.Arrays;
import java.util.Scanner;

public class t2020 {
    public static void main(String[] args) {
        //t1();
        //t2();
        //t3();
        //t4();
        //t5();
        //t6();
        //t7();
        t8();
//        String urlStr = "http://www.aaa.bbb/search?scope=ccc&q=ddd";
//        System.out.println(URLUtil.url(urlStr).getPath());
//        System.out.println(StrUtil.removeSuffix(urlStr, URLUtil.url(urlStr).getPath()));
//        System.out.println(StrUtil.removeSuffix("http://www.aaa.bbb/search?scope=ccc&q=ddd","/search"));
    }

    /**
     * 约数个数
     */
    public static void t1(){
        int n = 1200000;
        int cnt = 0;
        for (int i = 1; i <= n; i++) {
            if (n % i == 0) cnt++;
        }
        System.out.println(cnt);
    }

    /**
     * 蛇形填数
     * 注意，求计算矩阵中第20 行第20 列的数是多少，
     * 一定要注意开的矩阵的行、列数量一定至少要是20的两倍
     */
    public static void t2(){
        int n = 20,m=20;
        int[][] arr = new int[50][44];
        int row = 0,col = 1;
        int distance = 0;
        arr[0][0] = 1;
        int startNum = 2;
        while (true){
            if (arr[19][19] != 0){
                System.out.println(arr[19][19]);
                break;
            }
            int[] kept = func(arr,row,col,startNum,distance);
            row = kept[0];
            col = kept[1];
            startNum = kept[2];
            distance = distance == 0 ? 1 : 0;
        }
    }

    /**
     *
     * @param arr
     * @param row
     * @param col
     * @param startNum
     * @param distance：0 - 从上往下走 1 - 从下往上走
     * @return 返回走完后的row(0)，col(1),startNum(2)
     */
    public static int[] func(int[][] arr,int row,int col,int startNum,int distance){
        //从上往下走
        if (distance == 0){
            while (col >= 0 && row < arr.length){
                arr[row][col] = startNum;
                startNum++;
                col--;
                row++;
            }
            if (row == arr.length){
                row = arr.length - 1;
                col = col + 2;
            }else{
                col = 0;
            }
            return new int[]{row,col,startNum};
        }
        //从下往上走
        else{
            while (row >= 0 && col < arr[0].length){
                arr[row][col] = startNum;
                startNum++;
                row--;
                col++;
            }
            if (col == arr[0].length){
                col = arr[0].length - 1;
                row = row + 2;
            }else{
                row = 0;
            }
            return new int[]{row,col,startNum};
        }
    }

    /**
     * 成绩统计
     */
    public static void t3(){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int score = 0;
        int cnt1 = 0;
        int cnt2 = 0;
        for (int i = 0; i < n; i++) {
            score = in.nextInt();
            if (score >= 60) cnt1++;
            if (score >= 85) cnt2++;
        }
        //Double.
        System.out.println(Math.round(cnt1 * 100.0 / n) + "%");//
        System.out.println(Math.round(cnt2 * 100.0 / n) + "%");
    }

    /**
     * 跑步锻炼
     * 需要了解如何判断平年和闰年，平年和闰年2月的天数
     */
    public static void t4(){
        int year = 2000;
        int month = 1;
        int date = 1;
        int xingqi = 6;
        int res = 0;
        while (true){
            if (date == 1 || xingqi == 1) res += 2;
            else res += 1;
            if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
                date = date == 31 ? 1 : date + 1;
            }else if (month == 2 && isRun(year)){
                date = date == 29 ? 1 : date + 1;
            }else if (month == 2 && !isRun(year)){
                date = date == 28 ? 1 : date + 1;
            }else{
                date = date == 30 ? 1 : date + 1;
            }
            month = date == 1 ? month + 1 : month;
            year = month == 13 ? year + 1 : year;
            month = month == 13 ? 1 : month;
            xingqi = xingqi == 7 ? 1 : xingqi + 1;
            if (year == 2020 && month == 10 && date == 1 && xingqi == 4) break;
        }
        res += 2;
        System.out.println(res);
    }
    public static boolean isRun(int year){
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) return true;
        return false;
    }

    /**
     * 子串分值
     */
    public static void t5(){
        Scanner in = new Scanner(System.in);
        char[] chs = in.next().toCharArray();
        int[][] arrLeft = new int[chs.length][26];//arrLeft[n][i]表示chs的1-n范围内，最靠近n位置的字符i+'a'所在的位置
        int[][] arrright = new int[chs.length][26];//arrLeft[n][i]表示chs的n-length(chs)范围内，最靠近n位置的字符i+'a'所在的位置
        for (int i = 0; i < chs.length; i++) {
            arrLeft[i][chs[i]-'a'] = i;
            if(i == 0){
                for (int j = 0; j < 26; j++) {
                    if (chs[i]-'a' == j) continue;
                    arrLeft[i][j] = -1;//表示没出现过字符j+'a'
                }
            }else{
                for (int j = 0; j < 26; j++) {
                    if (chs[i]-'a' == j) continue;
                    arrLeft[i][j] = arrLeft[i - 1][j];
                }
            }
        }
        for (int i = chs.length - 1; i > -1; i--) {
            arrright[i][chs[i]-'a'] = i;
            if(i == chs.length - 1){
                for (int j = 0; j < 26; j++) {
                    if (chs[i]-'a' == j) continue;
                    arrright[i][j] = chs.length;//表示没出现过字符j+'a'
                }
            }else{
                for (int j = 0; j < 26; j++) {
                    if (chs[i]-'a' == j) continue;
                    arrright[i][j] = arrright[i + 1][j];
                }
            }
        }
        long res = 0;
        int left = 0;
        int right = 0;
        for (int i = 0; i < chs.length; i++) {
            if (i == 0){
                left = 1;
                right = arrright[i + 1][chs[i]-'a'] - i;
            }else if (i == chs.length - 1){
                right = 1;
                left = i - arrLeft[i - 1][chs[i]-'a'];
            }else{
                left = i - arrLeft[i - 1][chs[i]-'a'];
                right = arrright[i + 1][chs[i]-'a'] - i;
            }
            res += left * right;
        }
        System.out.println(res);
    }

    /**
     * 详见：https://blog.csdn.net/qq_45985728/article/details/119357469
     */
    public static void t6(){
        int[] g = new int[22];//g[i]：i个圆将平面划分的个数
        g[1] = 2;
        for (int i = 2; i <= 20; i++) {
            g[i] = g[i - 1] + 2 * (i - 1);
        }
        int[] f = new int[22]; //f[i]：代表i条直线 + 20条圆将平面划分的个数
        f[1] = g[20] + 2 * 20; //注意第一条直线此时没有与其他直线相交，因此需要单独处理
        for (int i = 2; i <= 20; i++) {
            f[i] = f[i - 1] + 2 * 20 + i;
        }
        System.out.println(f[20]);
    }

    /**
     * 倒序排列的字符串需要交换的次数最多，例如14个字符需要交换1+2+3+...+13=91次
     * 而14个字符需要交换1+2+3+...+13+14=105次，因此需要15个字符
     * 要求该字符串的字典序最小，那么我们最好第一个字母的字典序尽量小
     * 类比数列54321，若改为35421，则比3大的数字交换次数各减一，3与比3小的次数不变
     * 因此将onmlk j ihgfedcba中j往前移动5位，即jonmlkihgfedcba，次数变为100
     */
    public static void t7(){
        int n = 14;
        int cnt = 0;
        for (int i = 1; i < n; i++) {
            cnt += i;
        }
        System.out.println(cnt);
        String s = "";
        char[] arr = "jonmlkihgfedcba".toCharArray();
        System.out.println(sortCnt(arr));
        System.out.println(String.valueOf(arr));
    }
    public static int sortCnt(char[] arr){
        int cnt = 0;
        for (int i = arr.length - 1; i > 0; i--) {
            int maxIndex = i;
            for (int j = i - 1; j > -1; j--) {
                if (arr[maxIndex] < arr[j]) maxIndex = j;
            }
            System.out.println(arr[maxIndex]);
            if (maxIndex == i) continue;
            for (int j = maxIndex; j < i; j++) {
                char tmp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = tmp;
                cnt++;
            }
        }
        return cnt;
    }

    /**
     * 回文日期
     */
    public static void t8(){
        //isValid(2,35);
        //int countByMD = ;
        //ystem.out.println(getCountByMD(6, 21));
        int[] arr = new int[31 * 12];
        int len = 0;
        for (int month = 1; month <= 12; month++) {
            for (int day = 1; day <= 31; day++) {
                if (isValid(month,day)){
                    arr[len++] = getCountByMD(month,day);
                    //int countByMD = getCountByMD(month, day);
                    //System.out.println(countByMD);
                }
            }
        }
        for (int i = len; i < arr.length; i++) {
            arr[i] = Integer.MAX_VALUE;
        }
        Arrays.sort(arr);
        StringBuilder sb = new StringBuilder();
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        int n = 0;
        while (t-- > 0){
            n = in.nextInt();
            int res1 = 0;
            int res2 = 0;
            for (int i = 0; i < len; i++) {
                res1 = (res1 == 0 && arr[i] > n) ? arr[i] : res1;
                res2 = (res2 == 0 && arr[i] > n && isABA(arr[i])) ? arr[i] : res2;
                if (res1 != 0 && res2 != 0) break;
            }
            sb.append(res1 + "\n");
            sb.append(res2 + "\n");
        }
        System.out.print(sb.toString());
    }
    public static boolean isABA(int n){
        char[] s = String.valueOf(n).toCharArray();
        //System.out.println(String.valueOf(s));
        if (s.length < 8) return false;
        if (s[0] == s[2] && s[0] == s[5] && s[0] == s[7] && s[1] == s[3] && s[1] == s[4] && s[1] == s[6]) return true;
        else return false;
    }
    public static int getCountByMD(int month,int day){
        int year = day < 10 ? day * 10 : (day % 10 * 10 + day / 10);
        //System.out.println(year);
        year = (year * 100) + (month < 10 ? month * 10 : (month % 10 * 10 + month / 10));
        //System.out.println(year);
        year = year * 10000 + month * 100 + day;
        //System.out.println(year);
        return year;
    }
    public static boolean isValid(int month,int day){
        if (day == 10 || day == 20 || day == 30) return false;//会导致year不足4位
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
            return day <= 31;
        }else if (month == 2){
            int year = day < 10 ? day * 10 : (day % 10 * 10 + day / 10);
            //System.out.println(year);
            year = (year * 100) + (month < 10 ? month * 10 : (month % 10 * 10 + month / 10));
            //System.out.println(year);
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
                return day <= 29;
            }else {
                return day <= 28;
            }
        }else{
            return day <= 30;
        }
    }
}
